<?php

App::uses('AppController', 'Controller');

class CategoryController extends AppController {

	public $uses = array('Category');

	public function index() {

		$list = $this->Category->find('all', array('order' => 'id DESC'));
		$this->set('list', $list);
	}

	public function edit() {
		
		if ($this->request->is('post')) {
			
			$this->Category->save($this->request->data);
			$this->Session->setFlash('Update category success !', 'flash_custom');
			
			return $this->redirect('/category');
		} else {
			
			$id = $_GET['id'];
			$data = $this->Category->find('first', array('conditions' => array('id' => $id)));
			$this->set('data', $data);
		}
		
	}
	
	public function add() {

		if ($this->request->is('post')) {
				
			// If the form data can be validated and saved...
			if ($this->Category->save($this->request->data)) {

				// Set a session flash message and redirect.
				$this->Session->setFlash('Add new category success !', 'flash_custom');

				return $this->redirect('/category/add');
			}
		}
	}

	public function active() {

		$id = $_GET['id'];

		$data = array('id' => $id, 'delete_flg' => '0');

		// This will update Recipe with id 10
		$this->Category->save($data);

		return $this->redirect('/category');
	}

	public function delete() {

		$id = $_GET['id'];

		$data = array('id' => $id, 'delete_flg' => '1');

		// This will update Recipe with id 10
		$this->Category->save($data);

		return $this->redirect('/category');
	}
}
